package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.GuideAdapter;
import com.lyhengtongwl.zqsnews.base.Constant;
import com.lyhengtongwl.zqsnews.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 引导页
 */
public class GuideActivity extends AppCompatActivity {

    GestureDetector mGestureDetector;
    private int currentItem = 0;
    private int flaggingWidth;

    @BindView(R.id.viewpager)
    ViewPager viewPager;
    List<View> viewList = new ArrayList<>();
    GuideAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);

        slipToMain();
        //获取分辨率
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        flaggingWidth = dm.widthPixels / 10;

        initView();
    }

    private void initView() {
        View view1 = LayoutInflater.from(this).inflate(R.layout.guide1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.guide2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.guide3, null);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        mAdapter = new GuideAdapter(viewList);
        viewPager.setAdapter(mAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                currentItem = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }


    /**
     * 到广告页
     */
    private void slipToMain() {
        mGestureDetector = new GestureDetector(this,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2,
                                           float velocityX, float velocityY) {
                        if (currentItem == 2) {
                            if ((e1.getRawX() - e2.getRawX()) >= flaggingWidth) {
                                //用户不存在或者token不存在，就登录
                                boolean isShowAd = (boolean) SPUtils.get(getApplicationContext(),
                                        Constant.AdvertInfo.SHOW_ADVERT, false);
                                Intent intent_login;
//                                if (isShowAd) {
//                                    intent_login = new Intent(GuideActivity.this, AdvertActivity.class);
//                                } else {
                                    intent_login = new Intent(GuideActivity.this, NewsLoginActivity.class);
//                                }
                                startActivity(intent_login);
                                SPUtils.put(getApplicationContext(), Constant.CommonInfo.IS_FIRST, false);
                                finish();
                                return true;
                            }
                        }
                        return false;
                    }
                });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            mGestureDetector.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mGestureDetector != null) {
            if (mGestureDetector.onTouchEvent(ev)) {
                ev.setAction(MotionEvent.ACTION_CANCEL);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

}
